package com.xxzx.journalism.controller;

import com.xxzx.journalism.model.common.ResultModel;
import com.xxzx.journalism.model.vo.info.InfoListVO;
import com.xxzx.journalism.model.vo.info.InfoParamsVO;
import com.xxzx.journalism.model.vo.info.InfoVO;
import com.xxzx.journalism.model.vo.info.NavVO;
import com.xxzx.journalism.service.InfosService;
import com.xxzx.journalism.service.NavsService;
import com.xxzx.journalism.util.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * 信息管理
 */
@RequestMapping(path = "/infomgr", produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@Slf4j
public class InfomgrController {

    @Autowired
    InfosService infoService;

    @Autowired
    NavsService navService;

    /**
     * 信息管理列表
     *
     * @return
     */
    @GetMapping("")
    public ResponseEntity<ResultModel<List<InfoListVO>>> list(
            @NotEmpty @RequestParam String type
    ) {
        ResultModel<List<InfoListVO>> ret = new ResultModel<>();
        ret.setData(infoService.getAll(type));
        return ResultUtils.result(ret);
    }

    /**
     * 导航列表
     *
     * @return
     */
    @GetMapping("/navs")
    public ResponseEntity<ResultModel<List<NavVO>>> navs() {
        ResultModel<List<NavVO>> ret = new ResultModel<>();
        ret.setData(navService.getNavs());
        return ResultUtils.result(ret);
    }

    /**
     * 信息详情
     *
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<ResultModel<InfoVO>> detail(@Valid @NotEmpty @PathVariable String id) {
        ResultModel<InfoVO> ret = new ResultModel<>();
        ret.setData(infoService.detail(id));
        return ResultUtils.result(ret);
    }

    /**
     * 保存信息
     *
     * @return
     */
    @PostMapping("/save")
    public ResponseEntity<ResultModel<Integer>> save(@Valid @RequestBody InfoParamsVO infosParamsVO) {
        ResultModel<Integer> ret = new ResultModel<>();
        ret.setData(infoService.save(infosParamsVO));
        return ResultUtils.result(ret);
    }

    /**
     * 删除信息
     *
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<ResultModel<Boolean>> delete(@Valid @NotEmpty @PathVariable String id) {
        ResultModel<Boolean> ret = new ResultModel<>();
        ret.setData(infoService.removeById(id));
        return ResultUtils.result(ret);
    }
}